<?php

namespace app\service\admin;

use app\model\Common;
use app\tools\Logs;
use app\tools\Workerman;
use app\tools\WxOffiaccount;
use Exception;
use think\cache\driver\Redis;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Model;

/**
 * 首页
 *
 * user ：Du
 * Date : 2022/3/24
 * Time : 10:16
 * Class IndexService
 * @package projects\services\admin
 */
class IndexService
{

    /**
     * 获取首页信息
     *
     * User : Du
     * Date : 2022/3/24
     * Time : 10:20
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getData(): array
    {
        $webData = cache()->get('webData');
        if ($_SERVER['REMOTE_ADDR'] === '127.0.0.1' || !$webData) {
            $webData = Common::getData(WebsiteTable, ['id' => 1], ['name', 'desc', 'copyright', 'domain', 'icon']);
            cache()->set('webData', $webData);
        }
        $userInfo = session('manage_info');
        $menu = Common::getAllData(MenuTable, [], ['id', 'pid', 'name', 'name_en', 'icon', 'url'], true, 'sort', 'asc');
        $menu = getTree($menu['data']);
        return [
            'web_data' => $webData,
            'user_info' => $userInfo,
            'menu' => $menu,
        ];
    }

    /**
     * clientId绑定用户id
     * @param array $request
     * @return bool
     * @throws Exception
     */
    public static function noticeBindUser(array $request): bool
    {
        $clientId = $request['client_id'];
        if (!$clientId) {
            return false;
        }
        Workerman::joinGroup($clientId);
        $data = [
            'client_id' => $clientId,
            'id' => session('manage_info')['id'],
        ];
        Workerman::bingUser($data);
        updateOnline();
        return true;
    }

    /**
     * 用户基本信息
     * User : Du
     * Date : 2022/11/24
     * Time : 12:38
     * @return array|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function userinfo()
    {
        $id = session('manage_info')['id'];
        $res = Common::getData(AdminTable, ['id' => $id], ['account', 'avatar', 'mobile', 'email']);
        if ($res['avatar']) {
            $res['avatar'] = imagePath($res['avatar']);
        }
        return $res;
    }

    /**
     * 修改基本信息
     * User : Du
     * Date : 2022/11/22
     * Time : 17:41
     * @param array $request
     * @return int
     * @throws DbException
     */
    public static function basicUpdate(array $request): int
    {
        $id = session('manage_info')['id'];
        if ($request['password'] !== '') {
            $request['password'] = password_hash($request['password'], PASSWORD_BCRYPT);
        } else {
            unset($request['password']);
        }
        if ($request['avatar'] === '') {
            unset($request['avatar']);
        }
        return Common::updateData(UserTable, ['id' => $id], $request);
    }

    /**
     * 微信绑定信息
     * User : Du
     * Date : 2022/11/22
     * Time : 22:20
     * @return array|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getWxBind()
    {
        $data = session('manage_info');
        return Common::getData(UserTable, ['id' => $data['id']], ['wx_openid', 'wx_name']);
    }

    /**
     * 用户绑定链接
     * User : Du
     * Date : 2022/11/22
     * Time : 22:51
     * @return array
     */
    public static function userBind(): array
    {
        $data = [
            'type' => 2,
            'user_id' => session('manage_info')['id'],
        ];
        $id = Common::addData(WxQrTable, $data, true);
        $qrUrl = WxOffiaccount::getBindUrl($id);
        return [
            'id' => $id,
            'url' => $qrUrl
        ];
    }

    /**
     * 用户绑定扫码回调
     * User : Du
     * Date : 2022/11/23
     * Time : 14:59
     * @param array $request
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function userScanCode(array $request): string
    {
        Logs::info('wx扫码', ['data' => $request], '用户扫码');
        return WxOffiaccount::userCode($request);
    }

    /**
     * 获取用户绑定状态
     * User : Du
     * Date : 2022/11/23
     * Time : 16:15
     * @param array $request
     * @return array|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function bindingStatus(array $request)
    {
        $data = session('manage_info');
        return Common::getData(WxQrTable, ['id' => $request['id'], 'type' => 2, 'user_id' => $data['id']], ['status']);
    }

    /**
     * 用户微信解绑
     * User : Du
     * Date : 2022/11/23
     * Time : 17:18
     * @return int
     * @throws DbException
     */
    public static function userWxUnbind(): int
    {
        $data = session('manage_info');
        return Common::updateData(UserTable, ['id' => $data['id']], ['wx_openid' => '']);
    }
}